home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
090
/
byt85aug.arc
/
MAZE.PRO
< prev
next >
Wrap
Text File
|
1985-08-29
|
2KB
|
56 lines
/* Prolog representation of the treasure maze */
adjacent(cave_entrance,trolls). adjacent(cave_entrance,fountain).
adjacent(fountain,limbo). adjacent(fountain,food).
adjacent(fountain,bandits). adjacent(fountain,mermaids).
adjacent(bandits,treasure). adjacent(bandits,exit).
adjacent(food,treasure). adjacent(trolls,treasure).
adjacent(mermaid,exit). adjacent(treasure,exit).
avoid([trolls,bandits]). /* the places to stay away from */
/*
The following is a predicate to display a path through the
maze avoiding the Dangers and passing through the treasure room.
To invoke it use:
traverse(cave_entrance,exit).
*/
traverse(Here,There) :-
avoid(Dangers), /* grab the list of places where not to go */
path(Here,There,Dangers,[Here]).
/* see text for explanation of the path predicate below*/
path(Here,Here,Dangers,Trail) :-
member(treasure,Trail), /* make sure we go by the treasure room */
reverse_write(Trail). /* print the trail starting with Here */
path(From,To,Dangers,Trail) :-
( /* choose an Intermediate location */
adjacent(From,Intermediate)
;
adjacent(Intermediate,From)
),
not member(Intermediate,Dangers), /* is it a dangerous place? */
not member(Intermediate,Trail), /* have we been there before? */
path(Intermediate,To,Dangers,[Intermediate|Trail]). /* extend path */
/* next, print a list in reverse order */
reverse_write([]).
reverse_write([Head|Tail]) :-
reverse_write(Tail),
nl,
write(Head).
member(X,[X|_]).
member(X,[_|Y]) :-
member(X,Y).
Tail]) :-
reverse_write(Tail),
nl,
write(Head).
member(X,[X|_]).
member(X,[_|Y])